package com.uvlab.sitespot.security.Service;

import com.uvlab.sitespot.security.common.JwtUser;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.sql.*;
import java.util.ArrayList;

/**
 * Created by jys on 2016/7/6 0006.
 */

//TODO 可以考虑使用远程的user服务，根据name取得用户信息,然后再封装为jwtuser


public class JwtUserService {

    Connection coon;
    public JwtUserService() {
        this.init();
    }

    public void init() {

//        try {
//            coon = userdata.getConnection();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        String url="jdbc:mysql://localhost:3306/userdemo";
//        try {
//            Class.forName("com.mysql.jdbc.Driver").newInstance();
//            coon= DriverManager.getConnection(url, "root", "root");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        // using postgresql test
        String url="jdbc:postgresql://localhost:5432/user";
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            coon= DriverManager.getConnection(url, "postgres", "root");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public JwtUser getUserByname(String name) {
//        JwtUser ret =new JwtUser();
//        String sql1 = String.format("SELECT username,password,enabled FROM users WHERE username='%s'", name);
//        String sql2 = String.format("SELECT username,role FROM user_roles WHERE username='%s'", name);
//        try {
//            Statement st = coon.createStatement();
//            st.execute(sql1);
//            ResultSet rs;
//            rs = st.getResultSet();
//            if (rs.next()) {
//                ret.setUsername(name);
//                ret.setPassword(rs.getString("password"));
//                int en=rs.getInt("enabled");
//                if (en==1) {
//                    ret.setEnabled(true);
//                }
//                else {
//                    ret.setEnabled(false);
//                }
//            }
//            st.execute(sql2);
//            rs = st.getResultSet();
//            ArrayList<SimpleGrantedAuthority>gas=new ArrayList<>();
//            if (rs.next()) {
//                SimpleGrantedAuthority ga=new SimpleGrantedAuthority(rs.getString("role"));
//                gas.add(ga);
//            }
//            ret.setAuthorities(gas);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return ret;
//    }

    public JwtUser getUserByname(String name) {
        JwtUser ret =new JwtUser();
        String sql1 = String.format("SELECT email,password,enabled, roles FROM tran0420 WHERE email='%s'", name);
//        String sql2 = String.format("SELECT username,role FROM user_roles WHERE username='%s'", name);
        ArrayList<SimpleGrantedAuthority>gas=new ArrayList<>();
        try {
            Statement st = coon.createStatement();
            st.execute(sql1);
            ResultSet rs;
            rs = st.getResultSet();
            if (rs.next()) {
                ret.setUsername(name);
                ret.setPassword(rs.getString("password"));
                ret.setEnabled(rs.getBoolean("enabled"));
                Array roles=rs.getArray("roles");
                for(String role: (String[])roles.getArray()) {
                    SimpleGrantedAuthority ga=new SimpleGrantedAuthority(role);
                    gas.add(ga);
                }
            }
            ret.setAuthorities(gas);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
